﻿/******************************************************************************************************************************
 * Class that handles the manipulation of the XML file that is used to store the recipes for the RecipeBox application
 * Author: Morgan Davis
 * Northwestern Michigan College (student assignment)
 * Created: 27 November 2013
 * ****************************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecipeBox
{
    public static class RecipeXML
    {
        public static XDocument recipeOBJ; //the xdocument object used throughout the class to manipulate the xml file

        #region Methods

        //returns all of the recipes from the xml in a list of "Recipe" in order to be used by the application
        public static List<Recipe> GetAllRecipes()
        {   List<Recipe> recipeList = new List<Recipe>();
            var recipes = recipeOBJ.Element("Recipes").Descendants("Recipe").ToList();
            foreach (var item in recipes)
            {
                Recipe recipe = new Recipe(){ //create recipe object to be added to the list of recipes
                name = item.Element("Name").Value.ToString(),
                creditedCook = item.Element("Credited").Value.ToString(),
                ingredients = item.Element("Ingredients").Descendants("Ingredient").Select(x => x.Value).ToList(),
                instructions = item.Element("Instructions").Descendants("Instruction").Select(x => x.Value).ToList()
                };
                recipeList.Add(recipe);
 
            }
            return recipeList; //return list of recipes to the calling form
        }

        //calling form passes a recipe object, method then breaks it up and adds it as a recipe into the xml file
        public static void CreateRecipe(Recipe recipe)
        {
            recipeOBJ.Element("Recipes").Add(new XElement("Recipe",
                new XElement("Name", recipe.name),
                new XElement("Credited", recipe.creditedCook),
                new XElement("Instructions", (from x in recipe.instructions select new XElement("Instruction",x))),
                new XElement("Ingredients", (from x in recipe.ingredients select new XElement("Ingredient",x)))));
            recipeOBJ.Save("recipes.xml");
        }

        //used to "load" the xml file into the Xdocument object for manipulation
        public static void LoadXML()
        {
            recipeOBJ = XDocument.Load("recipes.xml");
        }

        //calling form sends a recipe object that is to be deleted, method then targets that specifc recipe in the xml
        //file and removes it.
        public static void RemoveRecipe(Recipe recipe)
        {
            XElement deleteThis = recipeOBJ.Element("Recipes").Descendants("Recipe").Where(x => x.Element("Name")
                .Value == recipe.name && x.Element("Credited").Value == recipe.creditedCook).Single();
            deleteThis.Remove();
            recipeOBJ.Save("recipes.xml");
        }
        
        #endregion
    }
}
